package com.juzipi.demo.jdbc;

import java.sql.*;

public class JdbcSelectDemo {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            //获取连接对象
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT", "zhangsan", "lsjxj");
            //定义sql
            String s = "select * from practice";
            //获取执行sql的对象
            statement = connection.createStatement();
            //执行sql
            resultSet = statement.executeQuery(s);
//            System.out.println(resultSet);
            //让游标向下移动一行
            //循环判断游标是否位于最后一行(是否有数据)
            while (resultSet.next()) {
                //获取数据
                int anInt = resultSet.getInt(1);
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                System.out.println(anInt+","+name+","+age);
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //释放资源
            JdbcSelectPractice.tryCatch(connection, statement, resultSet);
        }
    }
}
